; 递归版本
(define (sum term a next b)
    (if (> a b)
        0
        (+ (term a) (sum term (next a) next b))))

; 迭代版本 1
(define (sum term a next b)
    (define (iter term a next b result)
        (if (> a b)
            result
            (iter term (next a) next b (+ (term a) result))))
    (iter term a next b 0))

; 迭代版本 2
(define (sum term a next b)
    (define (iter a result)
        (if (> a b)
            result
            (iter (next a) (+ (term a) result))))
    (iter a 0))

; 测试过程
(define (cube x) (* x x x))
(define (next a) (+ a 1))

(display "\n========================================\n")
(display (sum cube 1 next 1000))
(display "\n========================================\n")
